
import cv2
import numpy as np

from src import enum

def get_boss_tmp(img):
    # 将图像从BGR转换为HSV色彩空间
    tmp = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # 定义你想要保留的颜色范围（在这个例子中是红色）
    lower_red = np.array([20, 200, 200])
    upper_red = np.array([30, 255, 255])

    # 根据颜色范围创建掩码并应用
    mask = cv2.inRange(tmp, lower_red, upper_red)
    result = cv2.bitwise_and(img, img, mask=mask)

    gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
    ret, binary = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
    return binary

tmp_img = cv2.imread("img/boss.png")
tmp_boss = get_boss_tmp(tmp_img)

def is_boss():
    img = get_boss_tmp(enum.new_pic.copy())
    result = cv2.matchTemplate(img, tmp_boss, cv2.TM_CCOEFF_NORMED)
    (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(result)
    if maxVal > 0.9:
        return True
    return False

def get_target(image):
    # 将图像从BGR转换为HSV色彩空间
    hsv = cv2.cvtColor(enum.new_pic.copy(), cv2.COLOR_BGR2HSV)

    # 定义你想要保留的颜色范围（在这个例子中是红色）
    shot_conf = enum.setting['target_hsv'].split('#')
    shot_arr = list(map(int, shot_conf[0].split(",")))
    lower_red = np.array(shot_arr)

    shot_arr = list(map(int, shot_conf[1].split(",")))
    upper_red = np.array(shot_arr)

    # 根据颜色范围创建掩码并应用
    mask = cv2.inRange(hsv, lower_red, upper_red)
    result = cv2.bitwise_and(image, image, mask=mask)

    gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)

    gray = cv2.medianBlur(gray, 5)

    # gray = closing(gray, 5)

    ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    cv2.imshow("Image1", binary)
    circles = cv2.HoughCircles(binary, cv2.HOUGH_GRADIENT, 1.5, 100)
    if circles is not None and len(circles) == 1:
        circles = np.round(circles[0, :]).astype("int")
        for (x, y, r) in circles:
            cv2.circle(image, (x, y), r, (0, 0, 0), 5)
            return [x, y, r]
    return None


def get_shot(image):
    # 将图像从BGR转换为HSV色彩空间
    hsv = cv2.cvtColor(enum.new_pic.copy(), cv2.COLOR_BGR2HSV)

    # 定义你想要保留的颜色范围（在这个例子中是红色）
    shot_conf = enum.setting['shot_hsv'].split('#')
    shot_arr = list(map(int, shot_conf[0].split(",")))
    lower_red = np.array(shot_arr)

    shot_arr = list(map(int, shot_conf[1].split(",")))
    upper_red = np.array(shot_arr)

    # 根据颜色范围创建掩码并应用
    mask = cv2.inRange(hsv, lower_red, upper_red)
    result = cv2.bitwise_and(image, image, mask=mask)
    gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
    ret, binary = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)

    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (50, 50))  # 椭圆结构
    binary = cv2.dilate(binary, kernel)  # 膨胀

    circles = cv2.HoughCircles(binary, cv2.HOUGH_GRADIENT, 9, 100)


    if circles is not None and len(circles) == 1:
        circles = np.round(circles[0, :]).astype("int")
        for (x, y, r) in circles:
            cv2.circle(image, (x, y), r, (255, 255, 255), 5)
            return [x, y, r]

    return None



def get_zhuang():
    # [y1:y2, x1:x2]
    template = enum.new_pic[334:393, 13:75]
    try:
        templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
        cv2.threshold(templateGray, 200, 255, cv2.THRESH_BINARY, templateGray)
        templateGray = cv2.medianBlur(templateGray, 5)

        contours, hierarchy = cv2.findContours(templateGray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        contours_len = len(contours)
        # print(contours_len)
        if 6 > contours_len > 1:
            return True
    except Exception as e:
        return False

    return False


def process_frame(image):
    p1=get_target(image)
    p2=get_shot(image)
    return image, p1, p2
